/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

/*
 * PanelPreferencesView.java
 *
 * Created on Sep 11, 2011, 9:04:54 PM
 */
package group.pals.mailgate.preferences.ui;

import group.pals.mailgate.ui.IPreferences;
import group.pals.mailgate.ui.JSpinnerMouseWheelHandler;
import group.pals.mailgate.ui.Utils;
import group.pals.mailgate.preferences.Preferences;
import group.pals.utils.sys.MessageBox;
import java.awt.Rectangle;
import java.awt.SystemTray;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author Hai Bison
 */
public class PanelPreferencesView extends javax.swing.JPanel implements IPreferences {

    private final Preferences _Preferences;

    /** Creates new form PanelPreferencesView */
    public PanelPreferencesView() {
        initComponents();

        this._Preferences = Preferences.getInstance();
//    labelPopupWindowDescription.setText(group.pals.mailgate.utils.Utils.getInstance()
//            .getLimitedTextWidthHtml(panelPopupWindow.getPreferredSize().width - 50,
//            "<html><b>Note:</b> Padding width is the width used to fit the e-mail"
//            + " messages displayed in popup window.",
//            false));
        initUI();
        initListeners();
    }

    private void initUI() {
        sliderMouseDblClick.setMinimum(100);
        sliderMouseDblClick.setMaximum(1000);
        sliderMouseDblClick.setPaintLabels(true);
        sliderMouseDblClick.setPaintTicks(true);
        sliderMouseDblClick.setMajorTickSpacing(100);
    }

    private void initListeners() {
        JSpinnerMouseWheelHandler mwh = JSpinnerMouseWheelHandler.getInstance();

        SpinnerNumberModel numberModel = new SpinnerNumberModel(
                Preferences.DefPopupWindowDelayTime, 0, 60 * 60 * 24, 30);
        txtPopupWindowDelayTime.setModel(numberModel);
        txtPopupWindowDelayTime.addMouseWheelListener(mwh);

        Rectangle desktopSize = Utils.getInstance().getDesktopSize();

        numberModel = new SpinnerNumberModel(Preferences.DefPopupWindowWidth,
                Preferences.DefPopupWindowWidth, desktopSize.width, 50);
        txtPopupWindowWidth.setModel(numberModel);
        txtPopupWindowWidth.addMouseWheelListener(mwh);

        numberModel = new SpinnerNumberModel(Preferences.DefPopupWindowHeight,
                Preferences.DefPopupWindowHeight, desktopSize.height, 50);
        txtPopupWindowHeight.setModel(numberModel);
        txtPopupWindowHeight.addMouseWheelListener(mwh);

        numberModel = new SpinnerNumberModel(Preferences.DefPopupWindowPaddingWidth, 0, desktopSize.width, 50);
        txtPopupWindowPaddingWidth.setModel(numberModel);
        txtPopupWindowPaddingWidth.addMouseWheelListener(mwh);

        sliderMouseDblClick.addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                final int Step = 100;
                //round the current value to nearest 100
                int value = sliderMouseDblClick.getValue() / Step * Step;
                //set that value
                if (e.getWheelRotation() > 0) {
                    if (value > sliderMouseDblClick.getMinimum()) {
                        sliderMouseDblClick.setValue(value - Step);
                    }
                } else {
                    if (value < sliderMouseDblClick.getMaximum()) {
                        sliderMouseDblClick.setValue(value + Step);
                    }
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroupPopupWindowPositions = new javax.swing.ButtonGroup();
        panelSystem = new javax.swing.JPanel();
        chkMinimizeOnStart = new javax.swing.JCheckBox();
        panelEmails = new javax.swing.JPanel();
        chkGroupEmailsByConversation = new javax.swing.JCheckBox();
        panelPopupWindow = new javax.swing.JPanel();
        chkPopupWindowEnableAnimation = new javax.swing.JCheckBox();
        txtPopupWindowDelayTime = new javax.swing.JSpinner();
        txtPopupWindowWidth = new javax.swing.JSpinner();
        txtPopupWindowHeight = new javax.swing.JSpinner();
        txtPopupWindowPaddingWidth = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        buttonPopupWindowPos0 = new javax.swing.JRadioButton();
        buttonPopupWindowPos1 = new javax.swing.JRadioButton();
        buttonPopupWindowPos3 = new javax.swing.JRadioButton();
        buttonPopupWindowPos2 = new javax.swing.JRadioButton();
        buttonPopupWindowPos4 = new javax.swing.JRadioButton();
        sliderMouseDblClick = new javax.swing.JSlider();

        setPreferredSize(new java.awt.Dimension(450, 400));
        setLayout(new java.awt.GridBagLayout());

        panelSystem.setBorder(javax.swing.BorderFactory.createTitledBorder("System:"));
        panelSystem.setPreferredSize(new java.awt.Dimension(400, 53));
        panelSystem.setLayout(new java.awt.GridBagLayout());

        chkMinimizeOnStart.setFont(new java.awt.Font("Dialog", 0, 12));
        chkMinimizeOnStart.setText("Minimize to system tray on start");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        panelSystem.add(chkMinimizeOnStart, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        add(panelSystem, gridBagConstraints);

        panelEmails.setBorder(javax.swing.BorderFactory.createTitledBorder("E-mails:"));
        panelEmails.setPreferredSize(new java.awt.Dimension(400, 61));
        panelEmails.setLayout(new java.awt.GridBagLayout());

        chkGroupEmailsByConversation.setFont(new java.awt.Font("Dialog", 0, 12));
        chkGroupEmailsByConversation.setText("Group e-mails by conversation");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        panelEmails.add(chkGroupEmailsByConversation, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(panelEmails, gridBagConstraints);

        panelPopupWindow.setBorder(javax.swing.BorderFactory.createTitledBorder("Popup window:"));
        panelPopupWindow.setPreferredSize(new java.awt.Dimension(400, 172));
        panelPopupWindow.setLayout(new java.awt.GridBagLayout());

        chkPopupWindowEnableAnimation.setFont(new java.awt.Font("Dialog", 0, 12));
        chkPopupWindowEnableAnimation.setText("Enable animation");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 0.5;
        panelPopupWindow.add(chkPopupWindowEnableAnimation, gridBagConstraints);

        txtPopupWindowDelayTime.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Delay time (seconds):", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 0.5;
        panelPopupWindow.add(txtPopupWindowDelayTime, gridBagConstraints);

        txtPopupWindowWidth.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Width:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.25;
        gridBagConstraints.weighty = 0.5;
        panelPopupWindow.add(txtPopupWindowWidth, gridBagConstraints);

        txtPopupWindowHeight.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Height:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.25;
        gridBagConstraints.weighty = 0.5;
        panelPopupWindow.add(txtPopupWindowHeight, gridBagConstraints);

        txtPopupWindowPaddingWidth.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Padding width:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.5;
        panelPopupWindow.add(txtPopupWindowPaddingWidth, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Position:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10))); // NOI18N
        jPanel1.setLayout(new java.awt.GridBagLayout());

        buttonGroupPopupWindowPositions.add(buttonPopupWindowPos0);
        buttonPopupWindowPos0.setFont(new java.awt.Font("Dialog", 0, 12));
        buttonPopupWindowPos0.setText("top-left");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        jPanel1.add(buttonPopupWindowPos0, gridBagConstraints);

        buttonGroupPopupWindowPositions.add(buttonPopupWindowPos1);
        buttonPopupWindowPos1.setFont(new java.awt.Font("Dialog", 0, 12));
        buttonPopupWindowPos1.setText("top-right");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_END;
        jPanel1.add(buttonPopupWindowPos1, gridBagConstraints);

        buttonGroupPopupWindowPositions.add(buttonPopupWindowPos3);
        buttonPopupWindowPos3.setFont(new java.awt.Font("Dialog", 0, 12));
        buttonPopupWindowPos3.setText("bottom-left");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_START;
        jPanel1.add(buttonPopupWindowPos3, gridBagConstraints);

        buttonGroupPopupWindowPositions.add(buttonPopupWindowPos2);
        buttonPopupWindowPos2.setFont(new java.awt.Font("Dialog", 0, 12));
        buttonPopupWindowPos2.setText("center");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(buttonPopupWindowPos2, gridBagConstraints);

        buttonGroupPopupWindowPositions.add(buttonPopupWindowPos4);
        buttonPopupWindowPos4.setFont(new java.awt.Font("Dialog", 0, 12));
        buttonPopupWindowPos4.setText("bottom-right");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_END;
        jPanel1.add(buttonPopupWindowPos4, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        panelPopupWindow.add(jPanel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(panelPopupWindow, gridBagConstraints);

        sliderMouseDblClick.setFont(new java.awt.Font("Dialog", 0, 12));
        sliderMouseDblClick.setBorder(javax.swing.BorderFactory.createTitledBorder("Tray icon mouse double-click sensitivity (milliseconds):"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        add(sliderMouseDblClick, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupPopupWindowPositions;
    private javax.swing.JRadioButton buttonPopupWindowPos0;
    private javax.swing.JRadioButton buttonPopupWindowPos1;
    private javax.swing.JRadioButton buttonPopupWindowPos2;
    private javax.swing.JRadioButton buttonPopupWindowPos3;
    private javax.swing.JRadioButton buttonPopupWindowPos4;
    private javax.swing.JCheckBox chkGroupEmailsByConversation;
    private javax.swing.JCheckBox chkMinimizeOnStart;
    private javax.swing.JCheckBox chkPopupWindowEnableAnimation;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panelEmails;
    private javax.swing.JPanel panelPopupWindow;
    private javax.swing.JPanel panelSystem;
    private javax.swing.JSlider sliderMouseDblClick;
    private javax.swing.JSpinner txtPopupWindowDelayTime;
    private javax.swing.JSpinner txtPopupWindowHeight;
    private javax.swing.JSpinner txtPopupWindowPaddingWidth;
    private javax.swing.JSpinner txtPopupWindowWidth;
    // End of variables declaration//GEN-END:variables

    /*----------------------------------------------------------------------------
     * IPreferences
     */
    public void loadPreferences() {
        chkMinimizeOnStart.setEnabled(SystemTray.isSupported());
        chkMinimizeOnStart.setSelected(_Preferences.isMinimizeOnStart());
        chkGroupEmailsByConversation.setSelected(_Preferences.isGroupEmailsByConversation());

        chkPopupWindowEnableAnimation.setSelected(_Preferences.isPopupWindowAnimationOn());
        txtPopupWindowDelayTime.setValue(_Preferences.getPopupWindowDelayTime() / 1000);
        txtPopupWindowWidth.setValue(_Preferences.getPopupWindowWidth());
        txtPopupWindowHeight.setValue(_Preferences.getPopupWindowHeight());
        txtPopupWindowPaddingWidth.setValue(_Preferences.getPopupWindowPaddingWidth());

        switch (_Preferences.getPopupWindowPosition()) {
            case 0: {
                buttonPopupWindowPos0.setSelected(true);
                break;
            }//0

            case 1: {
                buttonPopupWindowPos1.setSelected(true);
                break;
            }//1

            case 2: {
                buttonPopupWindowPos2.setSelected(true);
                break;
            }//2

            case 3: {
                buttonPopupWindowPos3.setSelected(true);
                break;
            }//3

            default: {
                buttonPopupWindowPos4.setSelected(true);
                break;
            }
        }

        sliderMouseDblClick.setValue(_Preferences.getMouseDblClickSensitivity());
    }

    public boolean storePreferences(boolean prepare) {
        int popupWindowDelayTime = (Integer) txtPopupWindowDelayTime.getValue();
        if (popupWindowDelayTime <= 0) {
            MessageBox.showErrMsg(this, null, "Popup window's delay time must be larger than 0");
            return false;
        }

        Utils utilsUI = Utils.getInstance();
        Rectangle desktopSize = utilsUI.getDesktopSize();
        int width = (Integer) txtPopupWindowWidth.getValue();
        int height = (Integer) txtPopupWindowHeight.getValue();
        if (width < Preferences.DefPopupWindowWidth || height < Preferences.DefPopupWindowHeight
                || width >= desktopSize.width || height >= desktopSize.height) {
            MessageBox.showErrMsg(this, null,
                    String.format("Popup window's dimension must be in range %dx%d - %dx%d",
                    Preferences.DefPopupWindowWidth, Preferences.DefPopupWindowHeight,
                    desktopSize.width, desktopSize.height));
            return false;
        }
        int paddingWidth = (Integer) txtPopupWindowPaddingWidth.getValue();
        if (paddingWidth < 0) {
            MessageBox.showErrMsg(this, null, "Popup window's padding width must be larger than 0");
            return false;
        }

        if (prepare) {
            return true;
        }

        _Preferences.setMinimizeOnStart(chkMinimizeOnStart.isSelected());
        _Preferences.setGroupEmailsByConversation(chkGroupEmailsByConversation.isSelected());
        _Preferences.setPopupWindowAnimationOn(chkPopupWindowEnableAnimation.isSelected());
        _Preferences.setPopupWindowDelayTime(popupWindowDelayTime * 1000);
        _Preferences.setPopupWindowWidth(width);
        _Preferences.setPopupWindowHeight(height);
        _Preferences.setPopupWindowPaddingWidth(paddingWidth);

        if (buttonPopupWindowPos0.isSelected()) {
            _Preferences.setPopupWindowPosition(0);
        } else if (buttonPopupWindowPos1.isSelected()) {
            _Preferences.setPopupWindowPosition(1);
        } else if (buttonPopupWindowPos2.isSelected()) {
            _Preferences.setPopupWindowPosition(2);
        } else if (buttonPopupWindowPos3.isSelected()) {
            _Preferences.setPopupWindowPosition(3);
        } else {
            _Preferences.setPopupWindowPosition(4);
        }

        _Preferences.setMouseDblClickSensitivity(sliderMouseDblClick.getValue());

        return true;
    }

    public void closeResources() {
        //do nothing
    }
}
